package com.lanou.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanou.hotel.entity.Type;
import com.lanou.hotel.mapper.HomeMapper;
import com.lanou.hotel.mapper.TypeMapper;
import com.lanou.hotel.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanou.hotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author msy
 * @since 2023-10-17
 */
@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private HomeMapper homeMapper;

    /**
     * 分页查询类型
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultVO<Map<String,Object>> getTypes(int pageNum, int pageSize) {
        Page<Type> typePage = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<Type> typeWrapper = new LambdaQueryWrapper<>();
        List<Type> typeList = typeMapper.selectList(typePage, typeWrapper);//获取类型集合
        long total = typePage.getTotal();//获取总数

        Map<String, Object> map = new HashMap<>();
        map.put("typeList",typeList);
        map.put("total",total);

        return ResultVO.success("获取成功",map);
    }

    /**
     * 关联删除数据
     * @param typeList
     */
    @Override
    public void removeByIdsLinkHome(List<Type> typeList) {
        homeMapper.deleteByTypeIds(typeList);
        typeMapper.deleteBatchIds(typeList);
    }
}
